package com.example.app1.util;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;

import com.example.app1.LogUtil;

/**
 * 播放控制 播放 暂停 停止 上一章 下一章
 */
public class HandleCommand {
    private static final String TAG = "HandleCommand";

    @SuppressLint("CommitPrefEdits")
    public static void play(SharedPreferences sharedPreferences, HandleTts handleTts, String itemId) {
        LogUtil.info(TAG, "play:" + itemId);
        // 从缓存中获取当前状态
        String status = sharedPreferences.getString(CacheKey.STATUS.name(), PlayStatus.READY.name());
        PlayStatus playStatus = PlayStatus.valueOf(status);
        switch (playStatus) {
            case PAUSE:
                if (sharedPreferences.getString(CacheKey.CURRENT_ID.name(), "").equals(itemId)) {
                    handleTts.tryPlayNextText();
                    break;  // 如果播放id没有变化，就恢复播放，否则播放其它章节
                }
            case PLAYING:
                LogUtil.info(TAG, "is already playing");
                if (!sharedPreferences.getString(CacheKey.CURRENT_ID.name(), "").equals(itemId)) {
                    // itemId不一样，那就跳转播放
                    stop(sharedPreferences, handleTts);
                } else {
                    break;
                }
//                handleTts.tryPlayNextText();
            case STOP:
            case READY:
                if (sharedPreferences.getString(CacheKey.NEXT_ID.name(), "").equals(itemId)) {
                    // 准备播放的章节itemId 等于 缓存中的nextId
                    boolean hasNext = handleTts.clearCurrentItem();  // 清空队列中的当前章节
                    if (hasNext) {
                        handleTts.tryPlayNextText();  // 继续播放
                    } else {
                        // 更新缓存，以触发加载和唤醒播放线程
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putString(CacheKey.PLAY_AFTER_REQ_SIGN.name(), itemId);
                        edit.putString(CacheKey.REQ_ID.name(), itemId);
                        int reqInc = sharedPreferences.getInt(CacheKey.REQ_INC.name(), 1);
                        reqInc++;
                        edit.putInt(CacheKey.REQ_INC.name(), reqInc);
                        edit.apply();
                    }
                } else {
                    handleTts.clearQueue();
                    // 更新缓存，以触发加载和唤醒播放线程
                    SharedPreferences.Editor edit = sharedPreferences.edit();

                    edit.putString(CacheKey.PLAY_AFTER_REQ_SIGN.name(), itemId);
                    edit.putString(CacheKey.REQ_ID.name(), itemId);
                    int reqInc = sharedPreferences.getInt(CacheKey.REQ_INC.name(), 1);
                    reqInc++;
                    edit.putInt(CacheKey.REQ_INC.name(), reqInc);
                    edit.commit();
                }
                break;
        }
    }

    /**
     * 指定播放位置
     * @param sharedPreferences 缓存工具类
     * @param handleTts tts处理
     * @param idx 播放位置索引
     */
    public static void playAt(SharedPreferences sharedPreferences, HandleTts handleTts, int idx) {
        LogUtil.info(TAG, "playAt:" + idx);
        // 从缓存中获取当前状态
        String status = sharedPreferences.getString(CacheKey.STATUS.name(), PlayStatus.READY.name());
        PlayStatus playStatus = PlayStatus.valueOf(status);
        switch (playStatus) {
            case PLAYING:
                stop(sharedPreferences, handleTts);
            case PAUSE:
            case STOP:
                String[] contents = CacheUtil.getContents(sharedPreferences);
                String currentId = sharedPreferences.getString(CacheKey.CURRENT_ID.name(), PlayStatus.READY.name());
                handleTts.matchQueueIdx(contents, idx, currentId);
                handleTts.tryPlayNextText();
                break;
            case READY:
                CacheUtil.writeMsg(sharedPreferences, false, "很奇怪，怎么会发生这个场景");
                break;
        }
    }

    @SuppressLint("CommitPrefEdits")
    public static void pause(SharedPreferences sharedPreferences, HandleTts handleTts) {
        LogUtil.info(TAG, "pause");
        String status = sharedPreferences.getString(CacheKey.STATUS.name(), PlayStatus.READY.name());
        PlayStatus playStatus = PlayStatus.valueOf(status);
        if (playStatus == PlayStatus.PLAYING) {
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString(CacheKey.READY_STATUS.name(), PlayStatus.PAUSE.name());
            edit.commit();
            handleTts.doPause();
        }
    }

    @SuppressLint("CommitPrefEdits")
    public static void stop(SharedPreferences sharedPreferences, HandleTts handleTts) {
        LogUtil.info(TAG, "stop");
        String status = sharedPreferences.getString(CacheKey.STATUS.name(), PlayStatus.READY.name());
        PlayStatus playStatus = PlayStatus.valueOf(status);
        if (playStatus == PlayStatus.PLAYING) {
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString(CacheKey.READY_STATUS.name(), PlayStatus.STOP.name());
            edit.commit();
            handleTts.doStop();
        }
    }

    @SuppressLint("CommitPrefEdits")
    public static void playNext(SharedPreferences sharedPreferences, HandleTts handleTts) {
        LogUtil.info(TAG, "playNext:");
        String nextId = sharedPreferences.getString(CacheKey.NEXT_ID.name(), null);
        if (nextId == null || nextId.trim().isEmpty() || "null".equals(nextId)) {
            LogUtil.info(TAG, "没有下一章");
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString(CacheKey.MSG.name(), "没有下一章");
            edit.apply();
            return;
        }
        String status = sharedPreferences.getString(CacheKey.STATUS.name(), PlayStatus.READY.name());
        PlayStatus playStatus = PlayStatus.valueOf(status);
        switch (playStatus) {
            case PLAYING:
                handleTts.doStop();
            case PAUSE:
            case READY:
            case STOP:
                play(sharedPreferences, handleTts, nextId);
                break;
        }
    }

    public static void playPre(SharedPreferences sharedPreferences, HandleTts handleTts) {
        LogUtil.info(TAG, "playPre");
        String preId = sharedPreferences.getString(CacheKey.PRE_ID.name(), null);
        if (preId == null || preId.trim().isEmpty() || "null".equals(preId)) {
            LogUtil.info(TAG, "没有上一章");
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString(CacheKey.MSG.name(), "没有上一章");
            edit.apply();
            return;
        }
        String status = sharedPreferences.getString(CacheKey.STATUS.name(), PlayStatus.READY.name());
        PlayStatus playStatus = PlayStatus.valueOf(status);
        switch (playStatus) {
            case PLAYING:
                handleTts.doStop();
            case PAUSE:
            case READY:
            case STOP:
                play(sharedPreferences, handleTts, preId);
                break;
        }
    }
}
